package com.xingkeduo.util.tool;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2019/3/20 14:52
 * @Created 情绪十分不稳定的Shadow
 */
public class ResponseMessage implements Serializable {

    private static final long serialVersionUID = -1541225842772584107L;

    /**
     * 是否成功
     */
    private int status;

    /**
     * 反馈数据
     */
    private Object data;

    /**
     * 反馈信息
     */
    private String msg;

    /**
     * 响应码 ,token失效、认证失败时的code为：401
     */
    private int code;

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", this.status);
        if (data != null)
            map.put("data", this.getData());
        if (msg != null)
            map.put("msg", this.getMsg());
        map.put("code", this.getCode());
        return map;
    }

    protected ResponseMessage( ) {
        this.code = 500;
        this.msg = "NONE";
        this.status = 0;
    }

    protected ResponseMessage(Object data) {
        this.code = 500;
        this.data = data;
        this.status = 0;
    }
    protected ResponseMessage(int status, Object data) {
        this.code = status==1 ? 200 : 500;
        this.data = data;
        this.status = status;
    }

    protected ResponseMessage(int status, Object data, int code) {
        this(status, data);
        this.code = code;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Object getData() {
        return data;
    }

    public ResponseMessage setData(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONStringWithDateFormat(this, "yyyy-MM-dd HH:mm:ss");
    }

    public int getCode() {
        return code;
    }

    public ResponseMessage setCode(int code) {
        this.code = code;
        return this;
    }

    public static ResponseMessage fromJson(String json) {
        return JSON.parseObject(json, ResponseMessage.class);
    }

    public String getMsg() {
        return msg;
    }

    public ResponseMessage setmsg(String msg) {
        this.msg = msg;
        return this;
    }

    public static ResponseMessage ok() {
        return ok(null);
    }

    public static ResponseMessage ok(Object data) {
        return new ResponseMessage(1, data).setCode(200).setmsg("success");
    }

    public static ResponseMessage ok(String msg){
        return new ResponseMessage(1,null).setCode(200).setmsg(msg);
    }
    public static ResponseMessage ok(Object data,String msg) {
        return new ResponseMessage(1,data).setCode(200).setmsg(msg);
    }

    public static ResponseMessage ok(int status, Object data,String msg){
        return new ResponseMessage(status,data).setCode(200).setmsg(msg);
    }

    public static ResponseMessage error(Object obj) {
        return new ResponseMessage(obj);
    }

    public static ResponseMessage error(String msg, int code) {
        return new ResponseMessage().setCode(code).setmsg(msg);
    }

    public static ResponseMessage error(String msg, int code,Object data) {
        return new ResponseMessage(data).setCode(code).setmsg(msg);
    }

    public static ResponseMessage error(ResultStatus resultStatus) {
        return  new ResponseMessage(resultStatus.getMessage()).setCode(resultStatus.getCode());
    }
}
